<?php
/**
 * 优惠卷管理
 */
class discountAction extends backendAction
{

    public function _initialize() {
        parent::_initialize();
        // $this->_mod = M('discount');
    }

    public function index(){
    	
    	$p = ($_GET['p'])?$_GET['p']:0;
    	// session('callback_url','http://'.$_SERVER['HTTP_HOST'].'/index.php?g=admin&m=discount&a=index&menuid=328&p='.$p);

    	$Discount = M('discount');
    	$list = $Discount->order('id desc')->page($p.',25')->select();
    	$this->assign('list',$list); // 赋值数据集
    	import("ORG.Util.Page"); // 导入分页类
    	$count = $Discount->count(); // 查诟满足要求癿总记录数
    	$Page = new Page($count,25); // 实例化分页类 传入总记录数和每页显示癿记录数
		$show = $Page->show(); // 分页显示输出
		$this->assign('page',$show); // 赋值分页输出
		$this->display(); // 输出模板
    }

  //   public function del(){
  // //   	$Discount = M("discount"); // 实例化 User 对象
		// // $res = $Discount->where('id='.(int)$_GET['id'] )->delete(); 
		// // header('Location:'.session('callback_url'));
		
  //   }

    public function add(){
    	$this->assign('code_num',uniqid());
        $this->assign('discount',0.8);
    	$this->display(); // 输出模板
    }


    public function save(){

    	$Discount = M("discount");
    	$data['code_num'] = trim($_POST['code_num']);
        $data['username'] = trim($_POST['username']);
    	$data['discount'] = trim($_POST['discount']);

    	if (!$data['code_num']) {
    		$this->error('请输入优惠码');
    	}

    	if (!$data['username']) {
    		$this->error('请输入销售人员');
    	}
        $item = $Discount->where(array('username'=>$data['username']))->find();
        if (!empty( $item )) {
            $this->error('已经为此销售人员添加过优惠码！');
        }

		$res = $Discount->data($data)->add();
		if ($res) {
			$this->success(L('operation_success'), U('index'));
		}else{
			$this->error(L('operation_failure'));
		}
    }


    public function edit(){
    	$Discount = M("discount");
    	$id = $_GET['id'];
    	$item = $Discount->where(array('id'=>$id))->find();
    	$this->assign('item',$item); // 赋值分页输出
		$this->display(); // 输出模板
    }

    public function update(){
    	$Discount = M("discount");
    	$id=$_POST['id'];
    	$data['username'] = trim($_POST['username']);
        $data['discount'] = trim($_POST['discount']);
    	$ref = $Discount->where(array('id'=>$id))->save($data);
		if($ref)
			$this->success(L('operation_success'), U('index'));
		else
			$this->error(L('operation_failure'));
    }

    public function use_record(){
        $p = ($_GET['p'])?$_GET['p']:1;

        $pageNum = 25;
        $index = ($p-1)*$pageNum;

        $id = $_GET['id'];
        $where = '';
        if ($id) {
            $where .= ' and d.id='.$id;
        }

        $list_sql = "SELECT o.`num`,o.`id` AS orderid ,o.`sn`,o.`goods`,o.`price`,o.`count`,o.`done_time`,o.`cate`,
                        CASE o.`type`
                        WHEN 0 THEN '未付款'
                        WHEN 1 THEN '等待发货'
                        WHEN 2 THEN '已经发货'
                        WHEN 3 THEN '完成'
                        END `type`,
                        d.`id` AS did,d.`code_num`,d.`username` FROM `wxs_item_orders` AS o ,`wxs_discount` AS d  WHERE o.`youhuiid`=d.`id` {$where} ORDER BY o.`id` DESC  LIMIT {$index},{$pageNum}";
        $list = M()->query($list_sql);


        $this->assign('list',$list); 
        import("ORG.Util.Page"); 
        $count_sql = "SELECT COUNT(o.id) AS total FROM `wxs_item_orders` AS o ,`wxs_discount` AS d  WHERE o.`youhuiid`=d.`id` " .$where;
        $count = M()->query($count_sql); 

        $count = $count[0]['total'];
        if ($id) {
            $Page = new Page($count,$pageNum,'id='.$id); 
        }else{
            $Page = new Page($count,$pageNum); 
        }
        $show = $Page->show(); 
        $this->assign('page',$show); 
        $this->display();
    }

}